# This file define the dictionary implementation. The dictionary is used to query
# where a word exists or not. Basically, 
# The dictionary provides the following function:

import sys

# A character. This can be a chinese or english character 
class ChChar:
    Value=''
 
# A chinese word or english word. This is the result of world segmentation   
# Value is a List here  
class ChWord:
    Value=[]

    # Return the position of the word
    def GetChar(self,index):
        return Value[index]     

# A prelearned dictionary. The dictionary is represent as a tree structure.
# It implements as a radix tree. The defintion of trie can see:
# http://en.wikipedia.org/wiki/Trie

class Dictionary:
    root={}
    
    def __init__(self):
        dictfile=open("dict.txt",r)
        for line in dictfile:
            self.AddWord(line)
        
    def AddWord(self,word):        
        node=self.root
        for char in word:
            node=self.AddNode(node,char)
        return
        
    def AddNode(self,node,char):
        if char not in node:
            dict={}
            node[char]=dict
        
        return node[char]
        
    def Exist(self,word):
        node=self.root
        ret=True
        for char in word:
            node=self.FindNode(node,char)
            if node==None:
                return False
        if len(node)>0:
            ret=False
        return ret
       
    def FindNode(self,node,char):
        if char in node:
            return node[char]
        else:
            return None

def TestMain():
#===============================================================================
#    dict=Dictionary()
#    a1=['a','b','c']
#    a2=['a','c']
#    a3=['b','c']
#    a4=['a','b']
#    a5=['c','c']
#    dict.AddWord(a1)
#    dict.AddWord(a2)
#    dict.AddWord(a3)   
#    print dict.Exist(a1)
#    print dict.Exist(a4)
#    print dict.Exist(a5)
#===============================================================================
    f=open('C:\\TDDOWNLOAD\\cedictu8\\cedict_ts.u8','rU')
    wf=open('C:\\TDDOWNLOAD\\cedictu8\\simcdict.u8','w+')
    for line in f:
        tokens=line.split(' ')
        wf.write(tokens[1])
        wf.write('\n')
        
if __name__=='__main__':
        TestMain()
        
            